﻿using System.Diagnostics;
using System.ServiceProcess;
using JetBlack.FeedBus.Distributor;
using JetBlack.Services;

namespace FeedBusDistributorService
{
    public partial class ServiceImpl : ServiceBase
    {
        private ManagedServer<Server> ManagedServer { get; set; }

        public ServiceImpl()
        {
            InitializeComponent();

            ManagedServer = new ManagedServer<Server>(new Server());

            if (EventLog != null)
                ManagedServer.Server.TraceSource.Listeners.Add(new EventLogTraceListener(EventLog));
        }

        protected override void OnStart(string[] args)
        {
            ManagedServer.Start(args);
        }

        protected override void OnStop()
        {
            ManagedServer.Stop();
        }

        private enum CustomCommand
        {
            ReportOff = 128,
            ReportCritical = 129,
            ReportError = 130,
            ReportWarning = 131,
            ReportInformation = 132,
            ReportVerbose = 133
        }

        protected override void OnCustomCommand(int command)
        {
            switch ((CustomCommand)command)
            {
                case CustomCommand.ReportOff:
                    ManagedServer.Server.TraceSource.Switch.Level = SourceLevels.Off;
                    break;
                case CustomCommand.ReportCritical:
                    ManagedServer.Server.TraceSource.Switch.Level = SourceLevels.Critical;
                    break;
                case CustomCommand.ReportError:
                    ManagedServer.Server.TraceSource.Switch.Level = SourceLevels.Error;
                    break;
                case CustomCommand.ReportWarning:
                    ManagedServer.Server.TraceSource.Switch.Level = SourceLevels.Warning;
                    break;
                case CustomCommand.ReportInformation:
                    ManagedServer.Server.TraceSource.Switch.Level = SourceLevels.Information;
                    break;
                case CustomCommand.ReportVerbose:
                    ManagedServer.Server.TraceSource.Switch.Level = SourceLevels.Verbose;
                    break;
                default:
                    base.OnCustomCommand(command);
                    break;
            }
        }
    }
}
